@isTest(SeeAllData=true)
private class CaseToWorkOrderHandlerTest {
    static testMethod void testMethod1() {

       // Id ldcProfile = [Select Id From Profile Where Name ='系统管理员' or Name='System Administrator'].Id;
        Id ldcProfile = [Select Id From Profile Where Name ='迈克经销商' ].Id;

    	 User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike',
                         Province__c = '四川省',
                         ManagerId = UserInfo.getUserId(),
                         CompanyName = 'Y-MKSY',
                         FederationIdentifier = '123451',
                         ResponseCode__c = 'TTTT' );
    	 insert u1;

             User u2= new User( Alias = 'samLdc2', 
                         Email = 'ldc2@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户2', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = [select Id from Profile where name like '%工程师%' limit 1].Id , 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123452',
                         CompanyName = 'Y-MKSY',
                         ManagerId = UserInfo.getUserId(),
                         Province__c = '四川省',
                         FederationIdentifier = '123452',
                         Username = 'Matldc2@test.com.maike',
                         ResponseCode__c = 'TTTT' );
             insert u2;
		//System.debug(u1);
    	
        User u = [select Id from User where Profile.Name = '系统管理员' and IsActive = true  limit 1];
        u.NewInstrumentAuthority__c = true;
        update u ;

        System.runAs(u) {
           Product__c p2 = TestUtils.createProduct();
            p2.Category0__c = '仪器';
            p2.PriceCompany__c = 'MKSY';
            p2.NCCode__c = '000002';
            p2.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
            insert p2;

            ResponsibleServer__c rs = new ResponsibleServer__c();
            rs.ResponseCode__c = 'TTTT';
             rs.PriorityUser__c = u1.Id;

            rs.WorkOnTime__c = Time.newInstance(9, 0, 0, 0);
            rs.WorkOffTime__c = Time.newInstance(17,30, 0, 0);
            rs.VacationStartDate__c = Date.newInstance(System.now().year(), System.now().month(), System.now().day() );
            rs.VacationEndDate__c = Date.newInstance(System.now().year(), System.now().month(), System.now().day() );

            rs.subCompany__c = 'MKSY';
            rs.ServiceType__c = '分销服务';

            insert rs;

            Account acc = TestUtils.createAccount();
            acc.Level__c  = '一级医院';
            acc.Name = 'CaseToWorkOrderHandlerTest';
            insert acc;

            Equipment__c eq = new Equipment__c();
            eq.ServiceAccount__c = rs.Id;
            eq.Account__c = acc.Id;
            eq.IMEINumber__c = 'xxx321';
            eq.Product__c = p2.Id ; 
            eq.FromFlow__c = 1;
            insert eq;



            Case ca = new Case();
            ca.AccountId = acc.Id;
            ca.Origin = '扫码报修';
    //
            ca.User__c = u2.Id ;
            ca.ServiceManufacture__c = eq.Id;
            ca.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('ServiceOrder', 'Case');
            ca.Faultcode__c = '11';
            ca.Failuretime__c = System.today();
            ca.FaultDeals__c= '部品更换';
            ca.Fault_Freq__c = '偶尔发生';
            ca.Maintenance_Info__c = 'xxxx';
            ca.ProcessResult__c = '待处理';
            ca.ReagentConsumables__c = '全部原装';
            ca.Reason_Desc__c = 'x';
            ca.Software_Version__c  = '1';
            ca.OperationCategory__c = '产品问题应对';
            ca.Faultlevel2__c= '硬件问题';
            ca.Faultlevel3__c = '机械';
            ca.OpponentStatus__c = '正常使用';
            //ca.TechExpense__c = 1;
            insert ca;

            
             System.Test.startTest();
             System.debug('====');
            ca.Engineer__c=u1.Id;

            update ca;

         
           WorkOrder__c wo =  [select Id ,Status__c from WorkOrder__c where Case__c =: ca.Id  limit 1 ];
           wo.Status__c = '完成';
           update wo;
            ca.Status = '关闭';
            try {
                update ca ; 
            }catch (Exception e) {}


           



        }
         


        
    }
    static testMethod void testMethod2() {

             Id ldcProfile = [Select Id From Profile Where Name ='迈克经销商' ].Id;

         User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         Username = 'Matldc1@test.com.maike',
                         Province__c = '四川省',
                         CompanyName = 'Y-MKSY',
                         ManagerId = UserInfo.getUserId(),
                         FederationIdentifier = '123451',
                         ResponseCode__c = 'TTTT' );
         insert u1;

             User u2= new User( Alias = 'samLdc2', 
                         Email = 'ldc2@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户2', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = [select Id from Profile where name like '%工程师%' limit 1].Id , 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123452',
                         CompanyName = 'Y-MKSY',
                         ManagerId = UserInfo.getUserId(),
                         Province__c = '四川省',
                         FederationIdentifier = '123452',
                         Username = 'Matldc2@test.com.maike',
                         ResponseCode__c = 'TTTT' );
             insert u2;

           Dealer__c d = new Dealer__c(
                Only__c='Dealer__c'
            );
            insert d;

             Account acc = TestUtils.createAccount();
            acc.Level__c  = '一级医院';
            acc.Name = 'CaseToWorkOrderHandlerTest';
            insert acc;


              //产品结构
            ProductStructure__c ps1=new ProductStructure__c();
            ps1.Name='仪器';
            ps1.Classify1__c='生化';
            ps1.Classify2__c='生化-设备';
            ps1.Classify3__c='仪器';
            insert ps1;
            Brand__c brand = new Brand__c(
                BrandShortName__c = '四川迈克'
            );
            insert brand;
              //产品
            Product__c p1=TestUtils.createProduct();
            p1.Name='仪器';
            p1.Category0__c = '仪器';
            p1.Model__c='Model1228';
            p1.ProductStructure__c=ps1.Id;
            p1.CRMBrand__c = brand.Id;
            p1.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
            insert p1;



              InstallationApply__c ins = new InstallationApply__c();
            ins.InstallationType__c='新装机';
            //必填字段 chatter 通知工作流引用到的字段都为必填字段
            ins.Instrument__c = p1.Id;
            ins.NEWDealer__c = d.Id;
            ins.Hospital__c = acc.Id;
            ins.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
            ins.InstallsNumber__c = 1;
            ins.EffectiveTime__c = System.now();
            insert ins;

             Case cas=new Case();
            cas.AccountId=acc.Id;
            cas.InstallationApply__c = ins.Id;
            cas.Engineer__c  = u1.Id;

             cas.Faultcode__c = '11';
            cas.Failuretime__c = System.today();
            cas.FaultDeals__c= '部品更换';
            cas.Fault_Freq__c = '偶尔发生';
            cas.Maintenance_Info__c = 'xxxx';

            insert cas;

            System.Test.startTest();
            


           cas.Status = '已分配';

            cas.Engineer__c  = u2.Id;
            update cas;

            System.debug('CaseToWorkOrderHandlerTest==>'+[select Id ,InstallationApply__c from Case where InstallationApply__c =: ins.Id ]);
                   cas.Status = '关闭';

           
            try {
                update cas ; 
            }catch (Exception e) {}

    }


}